﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using FubiManger.UI;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace FubiManger.Controls
{
    ////ToolboxItem用于控制是否添加自定义控件到工具箱，true添加，false不添加
    //[System.ComponentModel.ToolboxItem(true)]
    partial class diskcontrol : Smobiler.Core.Controls.MobileUserControl
    {
        /// <summary>
        /// 餐桌名称
        /// </summary>
        public string diskname { get; set; }
        /// <summary>
        /// 餐桌人数
        /// </summary>
        public string diskperson { get; set; }
        /// <summary>
        /// 编号
        /// </summary>
        public string diskid { get; set; }
        /// <summary>
        /// 商户号
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 商家logo
        /// </summary>
        public string logo { get; set; }
        /// <summary>
        /// 图片编号
        /// </summary>
        public string codeid { get; set; }
        public diskcontrol() : base()
        {
            //This call is required by the SmobilerUserControl.
            InitializeComponent();
        }

        private void diskcontrol_Load(object sender, EventArgs e)
        {
            codeid = Guid.NewGuid().ToString();
            lbldiskname.Text = diskname;//餐桌名称
            lbldisperon.Text = diskperson;//餐桌人数
        }

        private void panel1_LongPress(object sender, EventArgs e)
        {
            Adddiskcontrol con = new Adddiskcontrol();
            con.status = "1";//更新
            con.diskid = diskid;//编号
            con.diskname = lbldiskname.Text;//餐桌名称
            con.diskperson = lbldisperon.Text;//可容纳人数
            this.Form.ShowDialog(con);
        }

        private void pandel_Press(object sender, EventArgs e)
        {
            MessageBox.Show("点击确定将删除该餐桌信息", "删除提示", MessageBoxButtons.OKCancel, (obj, args) =>
            {
                if (args.Result == ShowResult.OK)
                {
                    if (diskDal.Deldisk(diskid) > 0)
                    {
                        ((EditdiskForm)this.Form).Refsh();//刷新
                    }
                }
            });
            
        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnqrcode_Press(object sender, EventArgs e)
        {
            //string disksn = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(diskname));
            string strCode = "http://su.funbusi.com:2924?parm=" + merchantid+","+diskid;

            Bitmap bit = code(strCode, 5, 7, @"C:\fbimage\" + logo, 20, 7, false);
            Bitmap bmp1 = new Bitmap(300, 350);
            Graphics g = Graphics.FromImage(bmp1);
            g.Clear(Color.White);
            g.Dispose();
            Bitmap bmp2 = (Bitmap)Imagimg(bmp1, bit);
            Bitmap bmp3 = (Bitmap)Drwintxt(bmp2, diskname);
            bmp3.Save(@"C:\fbcode\" + codeid+ ".png");
            this.Client.SaveImageToAlbum(codeid + ".png", @"C:\fbcode\");
            // MessageBox.Show(bit.Width.ToString() + "高：" + bit.Height);
        }
        public static Bitmap code(string msg, int version, int pixel, string icon_path, int icon_size, int icon_border, bool white_edge)
        {

            QRCoder.QRCodeGenerator code_generator = new QRCoder.QRCodeGenerator();

            QRCoder.QRCodeData code_data = code_generator.CreateQrCode(msg, QRCoder.QRCodeGenerator.ECCLevel.M/* 这里设置容错率的一个级别 */, true, true, QRCoder.QRCodeGenerator.EciMode.Utf8, version);

            QRCoder.QRCode code = new QRCoder.QRCode(code_data);

            //Bitmap icon = new Bitmap(icon_path);

            Bitmap bmp = code.GetGraphic(pixel, Color.Black, Color.White, null, icon_size, icon_border, white_edge);
            
            return bmp;

        }

        public static System.Drawing.Image Imagimg(Bitmap sourcePicture, Bitmap waterImage)
        {
            // 将需要加上水印的图片装载到Image对象中
            //
            System.Drawing.Image imgPhoto = sourcePicture;
            //
            // 确定其长宽
            //
            int phWidth = imgPhoto.Width;
            int phHeight = imgPhoto.Height;

            //
            // 封装 GDI+ 位图，此位图由图形图像及其属性的像素数据组成。
            //
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
            //
            // 定义一个绘图画面用来装载位图
            //
            Graphics grPhoto = Graphics.FromImage(bmPhoto);

            //
            //同样，由于水印是图片，我们也需要定义一个Image来装载它
            //
            System.Drawing.Image imgWatermark = waterImage;

            //
            // 获取水印图片的高度和宽度
            //
            int wmWidth = imgWatermark.Width;
            int wmHeight = imgWatermark.Height;
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.DrawImage(imgPhoto, 0, 0);
            grPhoto.DrawImage(waterImage, 20, 20);
            grPhoto.Dispose();
            imgPhoto = bmPhoto;

            return imgPhoto;
        }

        public static System.Drawing.Image Drwintxt(Bitmap product_images, string disksn)
        {

            System.Drawing.Image img = product_images;

            Graphics g = Graphics.FromImage(product_images);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            g.DrawImage(product_images, 0, 0, product_images.Width, product_images.Height);


            using (Font f = new Font("黑体", 20))//姓名
            {
                using (Brush b = new SolidBrush(Color.Black))
                {

                    g.DrawString("桌号:" + disksn, f, b, 20, 296);
                }
            }
           
            return img;
        }
    }
}